var http = require('http');
var url = require('url');

var cars = [{ name: 'BMW', price: 5500000, city: 'hangzhou' },
{ name: 'BZ', price: 5500000, city: 'hangzhou' },
{ name: 'BAT', price: 5500000, city: 'hangzhou' },
{ name: 'JL', price: 5500000, city: 'hangzhou' }]
http.createServer(function (request, response) {
    var pathobj = url.parse(request.url, true);
    switch (pathobj.pathname) {
        case '/cars':
            response.writeHead(200, { 'Content-Type': 'text/json' });
            response.end(JSON.stringify(cars));
            break;
        default:
            response.writeHead(200, { 'Content-Type': 'text/html' });
            response.end("<h1>请访问/cars</h1>".toString('utf-8'));
            break;
    }

}).listen(5001);

console.log('Server running at http://127.0.0.1:5001/cars');